package com.atguigu.spring6.aop.example;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyFactory {
    // 目标对象
    private Object target;
    public ProxyFactory(Object target) {
        this.target = target;
    }

    // 创建动态代理对象
    public Object getProxy() {
        /**
         * Proxy.newProxyInstance()方法参数
         * 1、获取类加载器
         * 2、获取接口数组
         * 3、获取增强器
         */
        InvocationHandler coluInvocationHandler = new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                System.out.println("【动态代理日志】" + method.getName() + "方法开始执行...");
                Object result = method.invoke(target, args);
                System.out.println("【动态代理日志】" + method.getName() + "方法执行结整..." + "结果：" + result);
                return result;
            }
        };
        return Proxy.newProxyInstance(target.getClass().getClassLoader(),
                                        target.getClass().getInterfaces(),
                                        coluInvocationHandler);
    }
}
